package ui.face {
	
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	import globals.Global;
	
	import ui.component.Button;
	import ui.component.Label;
	
	import util.DisplayUtil;
	import util.UIUtil;
	
	public class ActionIcon extends Sprite {
		
		private var upButton:Button;
		private var itemList:Array;
		public var opened:Boolean;
		private var iconBg:Bitmap;
		private var itemBg:Bitmap;
		private var overLayer:Sprite;
		private var _handler:Function;

		public function ActionIcon(iconUrl:String, handler:Function){
			super();
			
			overLayer = new Sprite();
			opened = false;
//			buttonMode = true;
			_handler = handler;
			
			iconBg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("sc_icon_bg"));
			var bitmap:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(iconUrl));
			bitmap.x = (54 - bitmap.width) / 2;
			bitmap.y = (54 - bitmap.height) / 2;
			addChild(bitmap);
			
			itemBg = new Bitmap(Global.assetsManager.getAssetsAsBitmapData("sc_bg"));
			itemBg.x = 0;
			itemBg.y = -154;
			overLayer.addChild(itemBg);
			addEventListener(MouseEvent.CLICK, toggleHandler);
			addEventListener(MouseEvent.ROLL_OVER, overHandler);
			addEventListener(MouseEvent.ROLL_OUT, outHandler);
			
			itemList = new Array();
		}
		
		public function addItem(iconUrl:String, labelText:String, handler:Function):void {
			var item:ActionItem = new ActionItem(iconUrl, labelText, handler);
			item.x = 0;
			item.y = -66 * (itemList.length + 1) - 14;
			overLayer.addChild(item);
			itemList.push(item);
		}
		
		public function layout(open:Boolean):void {
			opened = open;
			if (open) {
				addChildAt(overLayer, 0);
				addChildAt(iconBg, 0);
			} else {
				DisplayUtil.removeChild(this, overLayer);
				DisplayUtil.removeChild(this, iconBg);
			}
		}
		
		private function toggleHandler(event:MouseEvent):void {
			if (_handler != null) {
				_handler(this);
			}
		}

		private function overHandler(event:MouseEvent):void {
			addChildAt(iconBg, 0);
		}
		
		private function outHandler(event:MouseEvent):void {
			if (!opened) {
				DisplayUtil.removeChild(this, iconBg);
			}
		}
	}
}

import flash.display.Bitmap;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextFieldAutoSize;

import globals.Global;

import ui.component.Button;
import ui.component.Label;

class ActionItem extends Sprite {
	
	private var _handler:Function;
	
	public function ActionItem(iconUrl:String, labelText:String, handler:Function) {
		
		var bg:Bitmap = new Bitmap(Global.assetsManager.getAssetsAsBitmapData(iconUrl));
		bg.x = 28 - bg.width * 0.5;
		addChild(bg);
		
		var label:Label = new Label(12, 0xFFFFFF);
		label.x = 16;
		label.y = 36;
		label.text = labelText;
		label.height = 20;
		addChild(label);
		
		_handler = handler;
		
		buttonMode = true;
		addEventListener(MouseEvent.CLICK, clickHandler);
	}
	
	private function clickHandler(event:MouseEvent):void {
		if (_handler != null) {
			_handler();
		}
	}
}